package com.fr.report.script.function;

import com.fr.base.FRContext;
import com.fr.base.file.DatasourceManager;
import com.fr.data.core.db.DBUtils;
import com.fr.data.impl.DatabaseConnection;
import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;
import com.fr.report.script.core.FArray;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FIELDS extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    if (paramArrayOfObject.length != 2)
      return Primitive.ERROR_NAME;
    String str1 = paramArrayOfObject[0].toString();
    String str2 = paramArrayOfObject[1].toString();
    if ((str1 == null) || (str2 == null))
      return Primitive.NULL;
    com.fr.data.impl.Connection localConnection = FRContext.getDatasourceManager().getConnection(str2);
    if ((localConnection == null) || (!(localConnection instanceof DatabaseConnection)))
      return Primitive.NULL;
    DatabaseConnection localDatabaseConnection = (DatabaseConnection)localConnection;
    java.sql.Connection localConnection1 = null;
    try
    {
      localConnection1 = localDatabaseConnection.createConnection();
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    String[] arrayOfString = new String[0];
    try
    {
      String str3 = "select * from " + str1;
      arrayOfString = DBUtils.checkInColumnNames(localConnection1, str3, 0);
    }
    finally
    {
      try
      {
        if (localConnection1 != null)
          localConnection1.close();
      }
      catch (SQLException localSQLException)
      {
        FRContext.getLogger().log(Level.WARNING, localSQLException.getMessage(), localSQLException);
      }
    }
    return new FArray(arrayOfString);
  }

  public Function.Type getType()
  {
    return Function.REPORT;
  }

  public String getCN()
  {
    return "FIELDS(connectionName,tableName):返回tableName这个表中的所有字段名。\n示例：\n数据库BASE中有个名叫task的表的内容如下：\nname start  end\na    2008   2009\nb    2009   2012\n那么FIELDS(\"BASE\",\"task\")等于[name,start,end].";
  }

  public String getEN()
  {
    return "FIELDS(connectionName,tableName):returns the fields' name of tableName.\nExample：\nA table named task is in the database:BASE, the content of the table as follows:\nname start  end\na    2008   2009\nb    2009   2012\nthen FIELDARRAY \"BASE\",\"task\") returns [name,start,end].";
  }
}